package com.itheima.mq.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.pojo.entity.PublishInfo;
import com.itheima.commons.pojo.mongo.Publish;
import com.itheima.commons.utils.ContentCheckUtil;
import com.itheima.dubbo.interfaces.PublishApi;
import com.itheima.dubbo.interfaces.PublishInfoApi;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * @author yaohu
 * @date 2021/12/18
 * @Version 1.0
 * @describe
 */
@Component
@RocketMQMessageListener(consumerGroup = "cp1", topic = "check")
public class MessageListener implements RocketMQListener<String> {

    @Reference
    private PublishInfoApi publishInfoApi;

    @Reference
    private PublishApi publishApi;

    @Override
    public void onMessage(String message) {

        System.out.println("接到信息了check");

        PublishInfo publishInfo = JSONUtil.toBean(message, PublishInfo.class);

        Publish publish = publishApi.queryPublishById(publishInfo.getPublishId());
        // 自增 不传
        //publishInfo.setId(0L);
        // 默认待审核
        publishInfo.setState("1");

        String textCheck = ContentCheckUtil.textCheck(publish.getText());

        if (ObjectUtil.isNull(publish.getMedias())) {
            publishInfo.setState(textCheck);
        } else {
            String imagesCheck = ContentCheckUtil.imagesCheck(publish.getMedias());
            if (textCheck.equals(imagesCheck)) {
                publishInfo.setState(textCheck);
            } else if ("6".equals(textCheck) || "6".equals(imagesCheck)) {
                publishInfo.setState("6");
            } else {
                publishInfo.setState("3");
            }
        }

        publishInfo.setUserId(publish.getUserId());
        // 默认不置顶
        publishInfo.setIstop(1);
        publishInfo.setPublishId(publish.getId().toHexString());
        publishInfo.setCreated(System.currentTimeMillis());
        publishInfo.setUpdated(System.currentTimeMillis());

        publishInfo.setReportCount(0);
        publishInfo.setLikeCount(0);
        publishInfo.setCommentCount(0);
        publishInfo.setForwardingCount(0);

        publishInfoApi.savePublishInfo(publishInfo);
    }
}
